package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


import java.util.Arrays;
import java.util.List;
import java.util.UUID;

//套餐管理
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;
    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {

        try {
            String fileName = UUID.randomUUID().toString() + imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将文件名缓存到redis数据库
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            resource.close();
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        } finally {

        }
    }

    //    新增套餐方法
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    //套餐分页展示方法
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }
    //删除套餐方法
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            setmealService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除套餐失败");
        }
        return new Result(true,"删除套餐成功");
    }
    //根据套餐id查询关联检查组id
    @RequestMapping("/findGroupIdsbyId")
    public Result findGroupIdsbyId(int id){
        try {
           List<Integer> list=setmealService.findGroupIdsbyId(id);
           return new Result(true,"查询关联检查组数据成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询关联检查组数据失败");
        }
    }
    //编辑方法
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal, Arrays.asList(checkgroupIds));
        }catch (Exception e){
            return new Result(false,"编辑套餐失败");
        }
        return new Result(true,"编辑套餐成功");
    }
}

